import { BaseService, type PageResult } from '../../shared/services/base-service'
import type { ApiResponse } from '../../../types/api'
import type { Chapter, Character, Inspiration } from '../stores/book-data'
import type { CharacterProfile } from './book-role-service'

// 书籍实体类型
export interface Book {
  id?: number
  title: string
  description: string
  genre?: string // 题材，多个题材逗号分隔
  coverImage?: string
  status: number // 0-草稿, 1-已发布, 2-已下架
  userId: number
  createTime?: string
  updateTime?: string
}

// 分页查询参数
export interface BookQueryParams {
  page: number
  size: number
  title?: string
  status?: number
  userId?: number
}

// 生成封面参数
export interface CoverGenerationParams {
  message: string
  modelId: number
  num?: number
  width?: number
  height?: number
  weight?: number
  chatModelId?: number
  sysTemplateId?: number
}

// 书籍管理API服务
class BookService extends BaseService {
  // 获取书籍列表（分页）
  async getBooks(params: BookQueryParams): Promise<ApiResponse<PageResult<Book>>> {
    return this.get('/novel/book/list', params)
  }

  // 获取书籍详情
  async getBookById(id: number): Promise<ApiResponse<Book>> {
    return this.get(`/novel/book/${id}`)
  }

  // 创建书籍
  async createBook(book: Book): Promise<ApiResponse<Book>> {
    return this.post('/novel/book', book)
  }

  // 更新书籍
  async updateBook(book: Book): Promise<ApiResponse<Book>> {
    return this.put(`/novel/book/${book.id}`, book)
  }

  // 删除书籍
  async deleteBook(id: number): Promise<ApiResponse<string>> {
    return this.delete(`/novel/book/${id}`)
  }

  // 更新书籍状态
  async updateBookStatus(id: number, status: number): Promise<ApiResponse<string>> {
    return this.put(`/novel/book/${id}/status`, null, { params: { status } })
  }

  // 获取用户书籍列表
  async getUserBooks(userId: number, page = 1, size = 10): Promise<ApiResponse<PageResult<Book>>> {
    return this.get(`/novel/book/user/${userId}`, { page, size })
  }

  // 生成书籍封面
  async generateBookCover(params: CoverGenerationParams): Promise<ApiResponse<string>> {
    return this.post('/novel/book/cover', params)
  }

  // 章节相关API
  async getChaptersByBookId(bookId: number): Promise<ApiResponse<Chapter[]>> {
    return this.get(`/chapter/book/${bookId}`)
  }

  async getChapterByBookIdAndNumber(bookId: number, chapterNumber: number): Promise<ApiResponse<Chapter>> {
    return this.get(`/chapter/book/${bookId}/chapter/${chapterNumber}`)
  }

  async getChapterById(id: number): Promise<ApiResponse<Chapter>> {
    return this.get(`/chapter/${id}`)
  }

  async saveOrUpdateChapter(chapter: Chapter): Promise<ApiResponse<Chapter>> {
    return chapter.id 
      ? this.put(`/chapter/${chapter.id}`, chapter)
      : this.post(`/chapter/book/${chapter.bookId}`, chapter)
  }

  async createChapter(chapter: Chapter): Promise<ApiResponse<Chapter>> {
    return this.post(`/chapter/book/${chapter.bookId}`, chapter)
  }

  async updateChapter(chapter: Chapter): Promise<ApiResponse<Chapter>> {
    return this.put(`/chapter/${chapter.id}`, chapter)
  }

  async deleteChapter(id: number): Promise<ApiResponse<string>> {
    return this.delete(`/chapter/${id}`)
  }

  async generateChapterOutline(bookId: number, chapterNumber: number): Promise<ApiResponse<string>> {
    return this.post(`/chapter/book/${bookId}/chapter/${chapterNumber}/outline`)
  }

  async generateChapterContent(bookId: number, chapterNumber: number): Promise<ApiResponse<string>> {
    return this.post(`/chapter/book/${bookId}/chapter/${chapterNumber}/content`)
  }

  async getChapterOutlinesByBookId(bookId: number, pageNum: number = 1, pageSize: number = 10): Promise<ApiResponse<PageResult<Chapter>>> {
    return this.get(`/chapter/outline/book/${bookId}`, { pageNum, pageSize })
  }

  // 人物相关API
  async getCharacterProfilesByBookId(bookId: number): Promise<ApiResponse<Character[]>> {
    return this.get(`/novel/character-profile/book/${bookId}`)
  }

  async addCharacterProfile(data: Partial<CharacterProfile>): Promise<ApiResponse<CharacterProfile>> {
    return this.post('/book-roles', data)
  }

  async updateCharacterProfile(data: Partial<CharacterProfile>): Promise<ApiResponse<CharacterProfile>> {
    return this.put('/book-roles', data)
  }

  async deleteCharacterProfile(bookId: number, version: number): Promise<ApiResponse<any>> {
    return this.delete(`/book-roles/${bookId}/${version}`)
  }

  async createCharacter(character: Character): Promise<ApiResponse<Character>> {
    return this.post(`/novel/character-profile/book/${character.bookId}`, character)
  }

  async updateCharacter(character: Character): Promise<ApiResponse<Character>> {
    return this.put(`/novel/character-profile/book/${character.bookId}/character/${character.name}`, character)
  }

  async deleteCharacter(id: number): Promise<ApiResponse<string>> {
    return this.delete(`/novel/character-profile/${id}`)
  }

  // 灵感相关API
  async getInspirations(params: BookQueryParams): Promise<ApiResponse<PageResult<Inspiration>>> {
    return this.get('/novel/inspiration/list', params)
  }

  async getInspirationById(id: number): Promise<ApiResponse<Inspiration>> {
    return this.get(`/novel/inspiration/${id}`)
  }

  async createInspiration(inspiration: Inspiration): Promise<ApiResponse<Inspiration>> {
    return this.post('/novel/inspiration', inspiration)
  }

  async updateInspiration(inspiration: Inspiration): Promise<ApiResponse<Inspiration>> {
    return this.put(`/novel/inspiration/${inspiration.id}`, inspiration)
  }

  async deleteInspiration(id: number): Promise<ApiResponse<string>> {
    return this.delete(`/novel/inspiration/${id}`)
  }

  async getInspirationsByUserId(userId: number): Promise<ApiResponse<Inspiration[]>> {
    return this.get(`/novel/inspiration/user/${userId}`)
  }

  async getInspirationsByTags(tags: string): Promise<ApiResponse<Inspiration[]>> {
    return this.get(`/novel/inspiration/tags/${tags}`)
  }

  async getInspirationsByUserIdAndTags(userId: number, tags: string): Promise<ApiResponse<Inspiration[]>> {
    return this.get(`/novel/inspiration/user/${userId}/tags/${tags}`)
  }

  async getHotInspirations(): Promise<ApiResponse<Inspiration[]>> {
    return this.get('/novel/inspiration/hot')
  }

  async increaseInspirationUsage(id: number): Promise<ApiResponse<string>> {
    return this.put(`/novel/inspiration/${id}/usage`)
  }

  // 大纲相关API
  async generateNovelOutline(bookId: number): Promise<ApiResponse<string>> {
    return this.post('/novel/outline/generate', { bookId })
  }

  async getOutlineTaskStatus(taskId: string): Promise<ApiResponse<any>> {
    return this.get(`/novel/outline/task/${taskId}`)
  }

  async getOutlineTaskResult(taskId: string): Promise<ApiResponse<any>> {
    return this.get(`/novel/outline/task/${taskId}/result`)
  }

  async cancelOutlineTask(taskId: string): Promise<ApiResponse<string>> {
    return this.delete(`/novel/outline/task/${taskId}`)
  }

  async getOutlineByBookId(bookId: number): Promise<ApiResponse<any>> {
    return this.get(`/novel/outline/book/${bookId}`)
  }

  async getChapterOutlines(bookId: number): Promise<ApiResponse<any[]>> {
    return this.get(`/novel/outline/book/${bookId}/chapters`)
  }

  async deleteOutline(bookId: number): Promise<ApiResponse<string>> {
    return this.delete(`/novel/outline/book/${bookId}`)
  }

  async regenerateOutline(bookId: number): Promise<ApiResponse<string>> {
    return this.post(`/novel/outline/book/${bookId}/regenerate`)
  }

  // 书籍分析记录相关API
  async createBookAnalysisRecord(bookId: number): Promise<ApiResponse<any>> {
    return this.post('/book-analysis/records', { bookId })
  }

  async getBookAnalysisRecords(params: BookQueryParams): Promise<ApiResponse<PageResult<any>>> {
    return this.get('/book-analysis/records', params)
  }

  async getBookAnalysisRecordById(id: number): Promise<ApiResponse<any>> {
    return this.get(`/book-analysis/records/${id}`)
  }

  async updateBookAnalysisRecord(id: number, record: any): Promise<ApiResponse<any>> {
    return this.put(`/book-analysis/records/${id}`, record)
  }

  async deleteBookAnalysisRecord(id: number): Promise<ApiResponse<string>> {
    return this.delete(`/book-analysis/records/${id}`)
  }

  async restartBookAnalysis(id: number): Promise<ApiResponse<string>> {
    return this.post(`/book-analysis/records/${id}/restart`)
  }

  async getBookAnalysisStatus(id: number): Promise<ApiResponse<any>> {
    return this.get(`/book-analysis/records/${id}/status`)
  }

  async getBookAnalysisResults(id: number): Promise<ApiResponse<any>> {
    return this.get(`/book-analysis/records/${id}/results`)
  }
}

export const bookService = new BookService()